package com.example.gamestore.control;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.gamestore.Bean.Content;
import com.example.gamestore.Bean.Detail;
import com.example.gamestore.Bean.Game;
import com.example.gamestore.Bean.User;
import com.example.gamestore.aop.LogAnnotation;
import com.example.gamestore.mapper.ContentMapper;
import com.example.gamestore.mapper.GameMapper;
import com.example.gamestore.mapper.UserMapper;
import com.example.gamestore.service.ContentService;
import com.example.gamestore.service.DetailService;
import com.example.gamestore.service.GameService;
import com.example.gamestore.service.imp.ThreadService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
public class GameTableControl {
    @Autowired
   private GameService gameService;
    @Autowired
    private GameMapper gameMapper;
    @Autowired
    private DetailService detailService;
    @Autowired
    private ContentService contentService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ThreadService threadService;
    @GetMapping("/games")
    @LogAnnotation(module="游戏",operater="获取所有游戏")
    public String Games(@RequestParam(value = "pn",defaultValue = "1") Integer pn, Model model,
                        @RequestParam(value = "price",defaultValue = "hh") String price,
                        HttpServletRequest request){
        PageHelper.startPage(pn,6);
        LambdaQueryWrapper<Game> wrapper=new LambdaQueryWrapper<>();
        OrderByPrice(wrapper,price,model);
        wrapper.eq(Game::getStatus,1);
        List<Game> list = gameMapper.selectList(wrapper);
        PageInfo<Game> resultpage=new PageInfo<>(list,5);
        model.addAttribute("pages",resultpage);
        return "games";
    }

    @LogAnnotation(module="游戏",operater="游戏的详情信息")
    @GetMapping("/gamesdetails")
    public String gamesdetails(@RequestParam(value = "id",defaultValue = "1") Integer gameid,Model model){
        Detail detail=detailService.selectdetailbygameid(gameid);
        Game game=gameService.selectGameByid(gameid);
        List<Content> contentList=contentService.selectbyGameid(gameid);
        List<Content> reslutcontent=new ArrayList<>();
        for (Content content : contentList) {
            User user = userMapper.selectById(content.getAuthorId());
            content.setAuthorname(user.getNickname());
            reslutcontent.add(content);
        }
        model.addAttribute("detail",detail)
                .addAttribute("game",game)
                .addAttribute("contentlist",reslutcontent);
        //更新操作
        threadService.updateViewCounts(gameMapper,game);
        System.out.println(reslutcontent);
        return "games-details";
    }

    /**
     * 根据价格进行排序
     * @param wrapper
     * @param price
     * @param model
     */
    public void OrderByPrice(LambdaQueryWrapper<Game> wrapper,String price,Model model){
        if(!("hh".equals(price))){
            if("asc".equals(price)){
                wrapper.orderByAsc(Game::getPrice);
                model.addAttribute("price",price);
                System.out.println("asc");
            }
            if("desc".equals(price)){
                wrapper.orderByDesc(Game::getPrice);
                model.addAttribute("price",price);
                System.out.println("desc");
            }
        }

    }
}
